package com.ruicar.afs.cloud.apply.applytocontract;

import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractFeginParamEnum;
import com.ruicar.afs.cloud.manage.outer.api.common.dto.ApplyRecordConditionDto;
import com.ruicar.afs.cloud.manage.outer.api.common.dto.ManageUpcomeTasksRecordDto;
import com.ruicar.afs.cloud.manage.outer.api.common.feign.CommonFeign;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xudongdong
 * @date 2020-06-16
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ManageContractCommonController {

    private final CommonFeign commonFeign;

    private final ApplyConfig applyConfig;

    @ApiOperation(value = "根据合同号校验是否可以办理业务")
    @PostMapping("applyRecord/findByContractNo")
    public IResponse findByContractNo(@RequestBody ApplyRecordConditionDto applyRecordCondition) {
        return commonFeign.findByContractNo(applyRecordCondition, heards());
    }

    @ApiOperation(value = "根据合同号码、案件编号查询审批历史信息")
    @PostMapping("approveHistory/findByContractNo")
    public IResponse findByHistory(@RequestParam("contractNo") String contractNo, @RequestParam("caseNo") String caseNo) {
        return commonFeign.findByContractNo(contractNo, caseNo, heards());
    }

    @PostMapping(value = "findUpcomeTasks/findUpcomeTasksList")
    @ApiOperation(value = "待办任务一览")
    public IResponse findUpcomeTasksList(@RequestBody QueryCondition<ManageUpcomeTasksRecordDto> condition) {
        return commonFeign.findUpcomeTasksList(condition, heards());
    }

    private Map heards() {
        Map<String, String> heards = new HashMap<>(16);
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.BASIC_CLIENT_ID), applyConfig.getContractClientId());
        heards.put(AfsEnumUtil.key(ContractFeginParamEnum.BASIC_CLIENT_SECRET), applyConfig.getContractClientSecret());
        return heards;
    }
}
